@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM 微服务启动脚本 (Windows版本)
REM 作者: auth-cloud
REM 日期: 2024-01-01

REM 项目根目录
set "PROJECT_ROOT=%~dp0.."
set "PROJECT_ROOT=%PROJECT_ROOT:~0,-1%"
echo 项目根目录: %PROJECT_ROOT%

REM 检查Java环境
:check_java
java -version >nul 2>&1
if errorlevel 1 (
    echo 错误: 未找到Java环境，请先安装JDK 21
    exit /b 1
)
echo Java环境检查通过

REM 检查Maven环境
:check_maven
mvn -version >nul 2>&1
if errorlevel 1 (
    echo 错误: 未找到Maven环境，请先安装Maven
    exit /b 1
)
echo Maven环境检查通过

REM 编译项目
:build_project
echo 开始编译项目...
cd /d "%PROJECT_ROOT%"
call mvn clean package -DskipTests
if errorlevel 1 (
    echo 项目编译失败
    exit /b 1
)
echo 项目编译成功

REM 启动服务
:start_service
set "service_name=%~1"
set "service_port=%~2"
set "jar_file=%~3"

echo 启动服务: %service_name% (端口: %service_port%)

REM 检查端口是否被占用
netstat -an | findstr ":%service_port% " | findstr "LISTENING" >nul
if not errorlevel 1 (
    echo 端口 %service_port% 已被占用，请检查是否有其他服务在运行
    exit /b 1
)

REM 创建日志目录
if not exist "logs" mkdir logs

REM 启动服务
start "Service-%service_name%" java -jar "%jar_file%" > "logs\%service_name%.log" 2>&1

REM 等待服务启动
timeout /t 5 /nobreak >nul

echo 服务 %service_name% 启动完成

REM 停止服务
:stop_service
set "service_name=%~1"
echo 停止服务: %service_name%

REM 查找并停止服务进程
for /f "tokens=2" %%i in ('tasklist /fi "imagename eq java.exe" /fo csv ^| findstr /i "java.exe"') do (
    set "pid=%%i"
    set "pid=!pid:"=!"
    
    REM 检查进程是否在运行我们的服务
    wmic process where "ProcessId=!pid!" get CommandLine /format:list | findstr /i "%service_name%" >nul
    if not errorlevel 1 (
        echo 停止进程: !pid!
        taskkill /pid !pid! /f >nul 2>&1
    )
)

echo 服务 %service_name% 已停止

REM 检查服务状态
:check_service
set "service_name=%~1"
set "service_port=%~2"

netstat -an | findstr ":%service_port% " | findstr "LISTENING" >nul
if not errorlevel 1 (
    echo 服务 %service_name% 运行正常 (端口: %service_port%)
) else (
    echo 服务 %service_name% 未运行
)

REM 显示帮助信息
:show_help
echo.
echo 用法: %~nx0 [命令]
echo.
echo 命令:
echo   start-all     启动所有服务
echo   stop-all      停止所有服务
echo   restart-all   重启所有服务
echo   status        查看服务状态
echo   build         编译项目
echo   start [服务名] 启动指定服务
echo   stop [服务名]  停止指定服务
echo   help          显示帮助信息
echo.
echo 服务名:
echo   gateway       网关服务 (端口: 8080)
echo   registry      注册中心服务 (端口: 8081)
echo   auth-service  认证服务 (端口: 8082)
echo   user-service  用户服务 (端口: 8083)

REM 主函数
:main
if "%1"=="start-all" goto start_all
if "%1"=="stop-all" goto stop_all
if "%1"=="restart-all" goto restart_all
if "%1"=="status" goto status
if "%1"=="build" goto build
if "%1"=="start" goto start_single
if "%1"=="stop" goto stop_single
if "%1"=="help" goto show_help
if "%1"=="-h" goto show_help
if "%1"=="--help" goto show_help
goto show_help

:start_all
call :check_java
call :check_maven
call :build_project

echo 启动所有服务...
call :start_service "gateway" "8080" "auth-gateway\target\auth-gateway-1.0.0.jar"
call :start_service "registry" "8081" "auth-registry\target\auth-registry-1.0.0.jar"
call :start_service "auth-service" "8082" "auth-service\target\auth-service-1.0.0.jar"
call :start_service "user-service" "8083" "user-service\target\user-service-1.0.0.jar"

echo 所有服务启动完成
goto end

:stop_all
echo 停止所有服务...
call :stop_service "user-service"
call :stop_service "auth-service"
call :stop_service "registry"
call :stop_service "gateway"
echo 所有服务已停止
goto end

:restart_all
echo 重启所有服务...
call :stop_all
timeout /t 5 /nobreak >nul
call :start_all
goto end

:status
echo 服务状态:
call :check_service "gateway" "8080"
call :check_service "registry" "8081"
call :check_service "auth-service" "8082"
call :check_service "user-service" "8083"
goto end

:build
call :check_java
call :check_maven
call :build_project
goto end

:start_single
if "%2"=="" (
    echo 错误: 请指定服务名
    goto show_help
)

if "%2"=="gateway" (
    call :start_service "gateway" "8080" "auth-gateway\target\auth-gateway-1.0.0.jar"
) else if "%2"=="registry" (
    call :start_service "registry" "8081" "auth-registry\target\auth-registry-1.0.0.jar"
) else if "%2"=="auth-service" (
    call :start_service "auth-service" "8082" "auth-service\target\auth-service-1.0.0.jar"
) else if "%2"=="user-service" (
    call :start_service "user-service" "8083" "user-service\target\user-service-1.0.0.jar"
) else (
    echo 错误: 未知的服务名: %2
    goto show_help
)
goto end

:stop_single
if "%2"=="" (
    echo 错误: 请指定服务名
    goto show_help
)

call :stop_service "%2"
goto end

:end
endlocal
